package com.moon.icoresso.Service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.moon.icoresso.Exception.OauthException;
import com.moon.icoresso.Service.OauthRedisService;
import com.moon.icoresso.common.utils.StringRedisService;
import com.moon.icoresso.pojo.bo.OauthAccessTokenToRedisBO;
import com.moon.icoresso.pojo.bo.OauthCodeToRedisBO;
import com.moon.icoresso.pojo.bo.OauthRefreshTokenToRedisBO;
import com.moon.icoresso.pojo.bo.OauthToRedisBO;
import com.moon.icoresso.pojo.dto.OauthTokenDTO;
import com.moon.icoresso.pojo.dto.OauthUserInfo;
import com.moon.icoresso.pojo.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.moon.icoresso.pojo.constants.Constants4Oauth.MAX_EXPIRE_TIME;
import static com.moon.icoresso.pojo.constants.Constants4Oauth.OAUTH_CODE_REDIS_KEY_PREFIX;
import static com.moon.icoresso.pojo.constants.Constants4Oauth.USER_INFO_REDIS_PREFIX;

/**
 * @ClassName OauthServiceImlp
 * @Description
 * @Author menbbo
 * @Date 2021/2/12 17:03
 * @Version V1.0
 **/
@Service
public class OauthRedisServiceImpl implements OauthRedisService {
    @Autowired
    private StringRedisService<String,String> redisService;
    @Autowired
    private StringRedisService<String,Object> saveRedisService;
    @Autowired
    private StringRedisService<String,Object> redisObjectService;

    @Autowired
    private OauthRedisService oauthRedisService;
    @Override
    public OauthToRedisBO getClientId(String clientId) {
        String s = redisService.get(clientId);
        if(StringUtils.isEmpty(s)){
            throw new OauthException("未查询到clientId");
        }
        OauthToRedisBO oauthToRedisBO = JSONObject.parseObject(s, OauthToRedisBO.class);
        return oauthToRedisBO;
    }

    @Override
    public String generateUserInfoRedisKey(String userId) {
        return  USER_INFO_REDIS_PREFIX + userId;
    }

    @Override
    public void saveUserInfo(String key,OauthUserInfo oauthUserInfo,long time, TimeUnit timeUnit) {
        saveRedisService.set(key,oauthUserInfo,time,timeUnit);
    }

    @Override
    public void saveOauthCode(String code, String clientId, String tgc,String userInfoKey ) {
        OauthCodeToRedisBO oauthCodeToRedisBO = new OauthCodeToRedisBO();
        oauthCodeToRedisBO.setClientId(clientId);
        oauthCodeToRedisBO.setTgc(tgc);
        oauthCodeToRedisBO.setUserInfoRedisKey(userInfoKey);
        saveRedisService.set(OAUTH_CODE_REDIS_KEY_PREFIX+code,oauthCodeToRedisBO,MAX_EXPIRE_TIME,TimeUnit.SECONDS);
    }
    @Autowired
    private StringRedisService<String,OauthCodeToRedisBO> oauthCodeToRedisBOStringRedisService;
    @Override
    public OauthCodeToRedisBO findOauthCodeByCode(String code) {
        OauthCodeToRedisBO oauthCodeToRedisBO = oauthCodeToRedisBOStringRedisService.get(OAUTH_CODE_REDIS_KEY_PREFIX + code);
        if(oauthCodeToRedisBO==null){
          throw new OauthException("token不存在");
        }
    /*    OauthCodeToRedisBO oauthCodeToRedisBO = null;
        try{
             oauthCodeToRedisBO = JSONObject.parseObject(s, OauthCodeToRedisBO.class);
        }catch (Exception e){
            throw new OauthException("转JSON对象失败!");
        }*/
        return oauthCodeToRedisBO;
    }

    @Override
    public void saveAccessToken(String grantType,String clientId,String accessToekn, OauthUserInfo oauthUserInfo) {
        OauthAccessTokenToRedisBO oauthAccessTokenToRedisBO = new OauthAccessTokenToRedisBO();
        oauthAccessTokenToRedisBO.setOauthUserInfo(oauthUserInfo);
        oauthAccessTokenToRedisBO.setClientId(clientId);
        oauthAccessTokenToRedisBO.setGrantType(grantType);
        redisObjectService.set(accessToekn,oauthAccessTokenToRedisBO,MAX_EXPIRE_TIME);
    }

    @Override
    public void saveRefreshToken(String refreshToken,String grantType, String clientId, OauthUserInfo oauthUserInfo) {
        OauthRefreshTokenToRedisBO oauthRefreshTokenToRedisBO = new OauthRefreshTokenToRedisBO();
        if(oauthUserInfo!=null){
            //生成rediskey
            oauthRefreshTokenToRedisBO.setUserInfoToRedisKey(oauthRedisService.generateUserInfoRedisKey(oauthUserInfo.getUserName()));
        }
        oauthRefreshTokenToRedisBO.setClientId(clientId);
        oauthRefreshTokenToRedisBO.setGrantType(grantType);
        redisObjectService.set(refreshToken,oauthRefreshTokenToRedisBO,MAX_EXPIRE_TIME);
    }

    @Override
    public void deleteCode(String code) {
        redisService.delete(OAUTH_CODE_REDIS_KEY_PREFIX+code);
    }
}